<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Validator Chains - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.validate.validator_chains.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.validate.validator_chains.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.validate.set.html">Standard Validation Classes</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.validate.html">Zend_Validate</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.validate.writing_validators.html">Writing Validators</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.validate.validator_chains" class="section"><div class="info"><h1 class="title">Validator Chains</h1></div>
    

    <p class="para">
        Often multiple validations should be applied to some value in a particular order. The
        following code demonstrates a way to solve the example from the
        <a href="zend.validate.introduction.html" class="link">introduction</a>, where a username must be
        between 6 and 12 alphanumeric characters:
    </p>

    <pre class="programlisting brush: php">
// Create a validator chain and add validators to it
$validatorChain = new Zend_Validate();
$validatorChain-&gt;addValidator(
                    new Zend_Validate_StringLength(array(&#039;min&#039; =&gt; 6,
                                                         &#039;max&#039; =&gt; 12)))
               -&gt;addValidator(new Zend_Validate_Alnum());

// Validate the username
if ($validatorChain-&gt;isValid($username)) {
    // username passed validation
} else {
    // username failed validation; print reasons
    foreach ($validatorChain-&gt;getMessages() as $message) {
        echo &quot;$message\n&quot;;
    }
}
</pre>


    <p class="para">
        Validators are run in the order they were added to <span class="classname">Zend_Validate</span>. In
        the above example, the username is first checked to ensure that its length is between 6 and
        12 characters, and then it is checked to ensure that it contains only alphanumeric
        characters. The second validation, for alphanumeric characters, is performed regardless of
        whether the first validation, for length between 6 and 12 characters, succeeds. This means
        that if both validations fail,  <span class="methodname">getMessages()</span> will return failure
        messages from both validators.
    </p>

    <p class="para">
        In some cases it makes sense to have a validator break the chain if its validation process
        fails. <span class="classname">Zend_Validate</span> supports such use cases with the second
        parameter to the  <span class="methodname">addValidator()</span> method. By setting
        <var class="varname">$breakChainOnFailure</var> to <b><tt>TRUE</tt></b>, the added validator
        will break the chain execution upon failure, which avoids running any other validations that
        are determined to be unnecessary or inappropriate for the situation. If the above example
        were written as follows, then the alphanumeric validation would not occur if the string
        length validation fails:
    </p>

    <pre class="programlisting brush: php">
$validatorChain-&gt;addValidator(
                    new Zend_Validate_StringLength(array(&#039;min&#039; =&gt; 6,
                                                         &#039;max&#039; =&gt; 12)),
                    true)
               -&gt;addValidator(new Zend_Validate_Alnum());
</pre>


    <p class="para">
        Any object that implements <span class="classname">Zend_Validate_Interface</span> may be used in a
        validator chain.
    </p>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.validate.set.html">Standard Validation Classes</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.validate.html">Zend_Validate</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.validate.writing_validators.html">Writing Validators</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.validate.html">Zend_Validate</a></li>
  <li><a href="zend.validate.introduction.html">Introduction</a></li>
  <li><a href="zend.validate.set.html">Standard Validation Classes</a></li>
  <li class="active"><a href="zend.validate.validator_chains.html">Validator Chains</a></li>
  <li><a href="zend.validate.writing_validators.html">Writing Validators</a></li>
  <li><a href="zend.validate.messages.html">Validation Messages</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>